package me.zhengjie.modules.inspection.config;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.hutool.core.collection.CollectionUtil;
import me.zhengjie.modules.inspection.domain.InspectionPlan;
import me.zhengjie.modules.inspection.domain.TInspectionTask;
import me.zhengjie.modules.inspection.service.InspectionPlanService;
import me.zhengjie.modules.inspection.service.TInspectionTaskService;
import me.zhengjie.modules.inspection.service.dto.InspectionPlanQueryCriteria;
import me.zhengjie.modules.inspection.service.dto.TInspectionTaskDTO;
import me.zhengjie.modules.workorder.domain.TWorkOrder;
import me.zhengjie.modules.workorder.service.TWorkOrderService;

@Component
public class InspectionPlanJob {
	
	@Autowired
    private InspectionPlanService inspectionPlanService;
	
	@Autowired
	private TInspectionTaskService tInspectionTaskService;
	
	@Autowired
	private TWorkOrderService tWorkOrderService;
	
	@SuppressWarnings("unchecked")
	@Scheduled(cron = "0 0 0 */1 * ?")
	public void inspectionWorkOrder() {
		InspectionPlanQueryCriteria criteria = new InspectionPlanQueryCriteria();
		criteria.setState(1);
		List<InspectionPlan> plans = (List<InspectionPlan>) inspectionPlanService.queryAll(criteria);
		if(CollectionUtil.isNotEmpty(plans)) {
			Timestamp now = new Timestamp(System.currentTimeMillis());
			Date d = new Date(now.getTime());
			int month = Calendar.getInstance().get(Calendar.MONTH);
			plans.stream().forEach(plan->{
				if(plan.getStartTime().after(now) || plan.getEndTime().before(now)) {
					return;
				}
				Long periodId = plan.getPeriodId();
				TInspectionTask task = null;
				TWorkOrder order = null;
				TInspectionTaskDTO taskDto = null;
				switch (periodId.toString()) {
				case "1": //年
					if(month==0 && isFirstDayOfMonth(d)) {
						task = createInspectionTask(plan);
						taskDto = tInspectionTaskService.create(task);
						order = createTWorkOrder(taskDto);
					}
					break;
				case "2": //半年
					if(month==5 && isFirstDayOfMonth(d)) {
						task = createInspectionTask(plan);
						taskDto = tInspectionTaskService.create(task);
						order = createTWorkOrder(taskDto);
					}
					break;
				case "3": //季
					if((month==0 || month==3 || month==6 || month==9) && isFirstDayOfMonth(d)) {
						task = createInspectionTask(plan);
						taskDto = tInspectionTaskService.create(task);
						order = createTWorkOrder(taskDto);
					}
					break;
				case "4": //月
					if(isFirstDayOfMonth(d)) {
						task = createInspectionTask(plan);
						taskDto = tInspectionTaskService.create(task);
						order = createTWorkOrder(taskDto);
					}
					break;
				case "5": //周
					if(dayForWeek(d) ==1) {
						task = createInspectionTask(plan);
						taskDto = tInspectionTaskService.create(task);
						order = createTWorkOrder(taskDto);
					}
					break;
				default:
					break;
				}
				if(order!=null) {
					tWorkOrderService.create(order);
				}
			});
		}
	}
	
	private TInspectionTask createInspectionTask(InspectionPlan plan) {
		TInspectionTask task = new TInspectionTask();
		task.setPlanName(plan.getPlanName());
		task.setPlanid(plan.getId());
		task.setObjId(plan.getObjId());
		task.setPlanName(plan.getPlanName());
		task.setTemplateId(plan.getTemplateId());
		task.setTemplateName(plan.getTemplateName());
		task.setPlanDesc(plan.getPlanDesc());
		task.setPeriodId(plan.getPeriodId());
		task.setPeriodName(plan.getPeriodName());
		task.setInspectorId(plan.getInspectorId());
		task.setInspectorName(plan.getInspectorName());
		task.setVehTypeId(plan.getVehTypeId());
		task.setVehTypeName(plan.getVehTypeName());
		task.setStartTime(plan.getStartTime());
		task.setEndTime(plan.getEndTime());
		task.setState(plan.getState());
		return task;
	}
	
	private TWorkOrder createTWorkOrder(TInspectionTaskDTO taskDto) {
		TWorkOrder order = new TWorkOrder();
		order.setOrderSource(3);
		order.setSourceId(taskDto.getId());
		order.setServiceLevel(1);
		order.setFaultType(1);
		order.setSupplier(1L);
		return order;
	}
	
	/**
	 * 判断日期是否为1号
	 * @param date
	 * @return
	 */
	private boolean isFirstDayOfMonth(Date date){
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1);
	    if(calendar.get(Calendar.DAY_OF_MONTH) == 2){
	        return true;
	    }else{
	        return false;
	    }
	}
	
	/**
	 * 判断日期是周几
	 * @param date
	 * @return
	 * @throws Exception
	 */
	private int dayForWeek(Date date){
	    Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    int dayForWeek = 0;
	    if (c.get(Calendar.DAY_OF_WEEK) == 1) {
	        dayForWeek = 7;
	    } else {
	        dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
	    }
	    return dayForWeek;
	}

}
